/*global $*/
var MAIN = {};
if (typeof console === "undefined") {
    console = {log: function() {}};
}
$( function () {
    
    'use strict';
    MAIN.variables = {
        // These are the OpenLayers map layers defined in the initalize function
        layers: {},

        // This is the main openlayers map
        map: {},

        // OpenLayer controls
        controls: {},

        // This is the object that defines the projection into Lng/Lat
        lngLatProj: {},

        // This is used for the keyboard callback to delete selected
        // features
        selectedFeature: null,

        //Object to store user metadata.  Is probably something like name,
        //email, etc.
        userMetadata: {},
        
        //Object that stores information about default zoom and focus point
        viewport: {}
    };
    MAIN.dialogs = {};
    MAIN.callbacks = {};
    
    MAIN.callbacks.hoverFunction = function(div) {
        return function(e) {
            // on hover, show the element and set its position to hover above the mouse pointer
            var top = e.pageY - div.height() - 7;
            var left = e.pageX + 7;
            
            //This checks to see if the div scoots off the right hand side of the page
            //if so, slide it back to the left a bit
            var overlap = left + div.outerWidth() - $(window).width();
            if (overlap > 0) {
                left -= overlap;
            }
            div.show().css({
                position: 'absolute',
                top: top,
                left: left
            });
        };
    };
    
    // This function and following code makes sure the
    // map takes up most of the page to look good
    MAIN.callbacks.resizeFunction = function() {
        var mapHeight = $(window).height() - $("#info").outerHeight(true) -5;
        $("#map_div").height(mapHeight);
        if (MAIN.variables.map.updateSize) {
            MAIN.variables.map.updateSize();
            $.each(MAIN.variables.map.layers, function(unused, layer) {
                layer.redraw();
            });
        }
    };

    var project_settings_url = "data/project_settings.json";
    $.ajax({
        type: 'GET',
        url: project_settings_url,
        dataType: "json",
        async: false, // This is to get the callback to the userlayer set properly
        success: function(data) {
            MAIN.variables.cgi_path = data.cgi_web_path;
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.error("Couldn't fetch "+ project_settings_url);
            console.log(jqXHR);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
    
});